/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.mala.util;

/**
 *
 * @author joao
 */
import java.io.Serializable;
import java.sql.*;

public class Conecta implements Serializable {

    private static final long serialVersionUID = -4946249997765914170L;

    private Statement declaracao = null;
    private PreparedStatement declPreparada = null;
    private Connection conexao = null;
    private final ResultSet resultado = null;
    private int nErro = 0;
    private final String atualiza = "";
    private String url;
    private String password;
    private static final String JNDINome = "jdbc/acionistaDS";
    private final String driver = "net.sourceforge.jtds.jdbc.Driver";
    private final String endereco = "jdbc:jtds:sqlserver://localhost:1433/mala";
    private final String user = "mala";
    private final String pass = "mala";

    /**
     * Creates a new instance of Conecta
     *
     * @return
     * @throws java.sql.SQLException
     */
    public Connection Conecta() throws SQLException {
        try {
            if (conexao == null) {
                Class.forName(driver);
                conexao = DriverManager.getConnection(endereco, user, pass);
            }
            //conexao = getConexao(JNDINome);
            conexao.setAutoCommit(false);
            /*
             Class.forName("org.postgresql.Driver");
             conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/salao", "salao", "salao");
             //conexao = DriverManager.getConnection("jdbc:firebirdsql:192.168.1.99:/opt/firebird/data/apolo2.fdb?lc_ctype=WIN1252","sysdba","masterkey");
             conexao.setAutoCommit(false);
             declaracao = conexao.createStatement();
             * 
             */
        } catch (SQLException e) {
            System.out.println("Nao foi possivel Conectar!");
        } catch (ClassNotFoundException e) {
            System.out.println("Driver não carregado!");
        }
        return conexao;
    }

    public Connection getConexao(String JNDINome) throws Exception {
        if (conexao == null) {
            Class.forName(driver);
            conexao = DriverManager.getConnection(endereco, user, pass);
        }
        //conexao = getConexao(JNDINome);
        conexao.setAutoCommit(false);

        // Retorna a conexão
        return conexao;
    }

    public void close() throws SQLException {
        if (conexao != null) {
            conexao.close();
            conexao = null;
        }

    }

    public void fechar() {
        try {
            if (declPreparada != null) {
                declPreparada.close();
                declPreparada = null;
            }
            if (declaracao != null) {
                declaracao.close();
                declaracao = null;
            }

            if (conexao != null) {
                conexao.close();
                conexao = null;
            }
        } catch (SQLException e) {
            nErro = -1;
        }
    }
}
